package org.administer.contaroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.administer.dao.CrmCustomerDao;
import org.administer.dao.CrmDictionariesDao;
import org.administer.dao.CrmServiceDao;
import org.administer.pojo.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.ws.rs.POST;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Service")
public class ServiceController {
    @Autowired
    private CrmDictionariesDao crmDictionariesDao;
    @Autowired
    private CrmServiceDao crmServiceDao;
    @Autowired
    private CrmCustomerDao crmCustomerDao;

    /**
     * 创建服务
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping("/addService")
    public String addservice(HttpSession session) throws Exception{
      List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
      session.setAttribute("crmDictionaries",crmDictionaries);
      List<CrmCustomers> kehu=crmCustomerDao.getAllllist();
      session.setAttribute("kehu",kehu);
      return "/Service/addService";
    }
    @PostMapping("/addService")
    public Map<String,Object> addaddService(CrmService crmService,HttpSession session) throws Exception{
        Map<String,Object> map=new HashMap<>();
        //取出创建人的id
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        crmService.setCreator(crmUser.getId());
        crmServiceDao.add(crmService);
        return map;
    }

    /**
     * 服务分配
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping("/assignService")
    public String assignService(HttpSession session) throws Exception{
        List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
        session.setAttribute("crmDictionaries",crmDictionaries);
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        session.setAttribute("crmUser",crmUser);
        return "/Service/assignService";
    }
    @GetMapping("/getassign")
    @ResponseBody
    public Map<String,Object> assignget(HttpSession session,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmService crmService ) throws Exception{
        Map<String,Object> map=new HashMap<>();
//        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
//        crmService.setUserid(crmUser.getId());
//        System.out.println("分配人的id"+crmUser.getId());
        Page<CrmService> pager = PageHelper.startPage(page,limit);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",crmServiceDao.getlist(crmService));
        map.put("count",pager.getTotal());
        return map;
    }
    @RequestMapping("/search")
    @ResponseBody
    public List<CrmUser> search(HttpSession session) throws Exception{
        //取出创建人的id
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        return crmServiceDao.isnotid(crmUser.getId());
    }


    @RequestMapping("/deleteService")
    @ResponseBody
    public Map<String, Object> delete(Integer id) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmServiceDao.del(id);
        map.put("result",true);
        return map;
    }
    @PostMapping("/addfenpei")
    @ResponseBody
    public Map<String,Object> addfenpei(CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmServiceDao.update(crmService);
        return map;
    }
    /**
     * 服务归档
     */
    @GetMapping("/guidang")
    public String guidang(HttpSession session) throws Exception{
        List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
        session.setAttribute("crmDictionaries",crmDictionaries);
        return "/Service/chuliService";
    }
    @RequestMapping("/chulineirong")
    @ResponseBody
    public Map<String,Object> guidangneirong(HttpSession session,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmService crmService) throws Exception{
        CrmUser c= (CrmUser) session.getAttribute("login_user");
        crmService.setUserid(c.getId());
//        System.out.println("服务处理的id"+c.getId());
        System.out.println("aasdasd"+crmService.getUserid());
        Map<String,Object> map=new HashMap<>();
//        System.out.println("服务处理");
        Page<CrmService> pager1 = PageHelper.startPage(page,limit);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",crmServiceDao.getlistguidang(crmService));
        map.put("count",pager1.getTotal());
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        session.setAttribute("crmUser",crmUser);
        return map;
    }
    @GetMapping("/view")
    @ResponseBody
    public CrmService view(Integer id) throws Exception{
        return  crmServiceDao.get(id);
    }
    @PostMapping("/chuliservice")
    @ResponseBody
    public Map<String,Object> servicechuli(CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmServiceDao.updatechuli(crmService);
        return map;
    }


    /**
     * 服务反馈
     */
    @GetMapping("/fankui")
    public String fankui(HttpSession session) throws Exception{
        List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
        session.setAttribute("crmDictionaries",crmDictionaries);
        List<CrmDictionaries> manyidu= crmDictionariesDao.gettype("客户满意度");
        session.setAttribute("manyidu",manyidu);
        return "/Service/fankuiService";
    }
    @RequestMapping("/fankuineirong")
    @ResponseBody
    public Map<String,Object> fankuineirong(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmService> pager1 = PageHelper.startPage(page,limit);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",crmServiceDao.getlistfankui(crmService));
        map.put("count",pager1.getTotal());
        return map;
    }
    @PostMapping("/chulistat")
    @ResponseBody
    public Map<String,Object> chulistat(CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmServiceDao.updatestat(crmService);
        return map;
    }
    @PostMapping("/fankuiupdate")
    @ResponseBody
    public Map<String,Object> fankuiupdate(CrmService crmService,HttpSession session) throws Exception{
        CrmUser crmUser=(CrmUser) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<>();
        crmService.setConductor(crmUser.getId());
        System.out.println("处理人"+crmService.getConductor());
        System.out.println("处理时间 "+crmService.getConductorTime());
        System.out.println("结果"+crmService.getFeedback());
        System.out.println("满意度"+crmService.getSatisfaction());
        crmServiceDao.updatefankui(crmService);
        return map;
    }

    /**
     * 归档服务
     */
    @GetMapping("/archive")
    public String archive(HttpSession session) throws Exception{
        List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
        session.setAttribute("crmDictionaries",crmDictionaries);
        return "/Service/guidangService";
    }
    @RequestMapping("/archiveservice")
    @ResponseBody
    public Map<String,Object> archiveservice(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmService> pager1 = PageHelper.startPage(page,limit);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",crmServiceDao.archiveservice(crmService));
        map.put("count",pager1.getTotal());
        return map;
    }
    @PostMapping("/archiveupdate")
    @ResponseBody
    public Map<String,Object> archiveupdate(CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        crmServiceDao.updatefankui(crmService);
        return map;
    }
    /**
     * 查看服务
     */
    @GetMapping("/chakan")
    public String chakan(HttpSession session) throws Exception{
        List<CrmDictionaries> crmDictionaries= crmDictionariesDao.gettype("服务类型");
        session.setAttribute("crmDictionaries",crmDictionaries);
        return "/Service/chakanService";
    }
    @RequestMapping("/chakanfuwu")
    @ResponseBody
    public Map<String,Object> chakanfuwu(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmService crmService) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmService> pager1 = PageHelper.startPage(page,limit);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",crmServiceDao.chakanservice(crmService));
        map.put("count",pager1.getTotal());
        return map;
    }
}
